package com.taoge.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @author weipt
 * @description 多数据源配置
 * @date 2024/7/19 23:00
 */
@Configuration
@EnableTransactionManagement  //启用Spring的声明式事务管理
//通过MapperScan，将mapper路径与sqlSessionFactory绑定，后续我们注入mapper时，就不需要指定数据源
@MapperScan(basePackages = "com.taoge.zhzd.mapper", sqlSessionFactoryRef = "sqlSessionFactoryZhzd")
@MapperScan(basePackages = "com.taoge.zhhd.mapper", sqlSessionFactoryRef = "sqlSessionFactoryZhhd")
public class DataSourceConfig {

    @Autowired
    private Environment env;

    /**************************************多数据源实战-初始化数据库连接对象 start**************************************/
    @Bean(name = "zhzdDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.zhzd")  //因为druid中，已经有了完整的数据库连接信息，所以这种写法最简洁
    public DataSource zhzdDataSource() {
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }

    @Bean(name = "zhhdDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.zhhd")  //该注解会在你创建完类的时候，调用spring的钩子函数，将yml配置设置给返回的对象属性
    public DataSource zhhdDataSource() {
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }
    /**************************************多数据源实战-初始化数据库连接对象 end**************************************/

    /**************************************多数据源实战-初始化sqlSessionFactory并与数据源绑定 start**************************************/
    @Bean(name = "sqlSessionFactoryZhzd")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("zhzdDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        //factoryBean.setTypeAliasesPackage(env.getProperty("mybatis.type-aliases-package"));
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/zhzd/*.xml"));
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        factoryBean.setConfiguration(configuration);
        return factoryBean.getObject();
    }

    @Bean(name = "sqlSessionFactoryZhhd")
    public SqlSessionFactory zhhdSqlSessionFactory(@Qualifier("zhhdDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        //factoryBean.setTypeAliasesPackage(env.getProperty("mybatis.additional-type-aliases-package"));
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/zhhd/*.xml"));
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        return factoryBean.getObject();
    }
    /**************************************多数据源实战-初始化sqlSessionFactory并与数据源绑定 end**************************************/
}
